When you are developing multiple plugins, you might develop a library that will be used in some of them. So, what happens when your customers might use more than 1 of your plugins with that libary.
How are you going to handle the loading of it so you’re sure that the latest is being used?
In one of the previous tutorials, you could read about Using Composer for Libraries in WordPress Plugins or Themes.
This can be a way to load your libraries but you can’t control the loading order and which version of the library will load.
By only using the autoload.php
provided by composer, if your oldest library is loaded first, all your plugins will use that one. That might not be ideal.
So let’s now see how we can load libraries so all your plugins use the latest one.
Creating the Integration Class
Create a Library Integration class which, for the purpose of this tutorial, I’ll call Library_Integration
.
<?php
class Library_Integration {
/**
* Library Version
*
* @var string
*/
protected $version = '1.0.0';
/**
* Library Name
*
* @var string
*/
protected $name = 'my_library';
}
The $version
will define the library version that is included with your WordPress plugin. The $name
is the id of your library which we can use to load it.
In the method register_version
we will register our version inside of the $_GLOBALS
(you can change this with a different way of registering them if you need). If the library version used by this plugin is already registered, we won’t register it again.
What are we actually doing is that we are mapping the path to the library with the version.
<?php
class Library_Integration {
// previous code here ...
/**
* Get the path to the library
*/
public function get_path() {
return 'path/to/main/library.php';
}
}
Inside of the get_path
method, you will set the complete path to your library file. For example, if your library is loaded through composer, you could:
- define the main plugin path inside of your main plugin file using
define( 'MY_PLUGIN_PATH', plugin_dir_path(__FILE__));
- then inside of the get_path, set it like:
trailingslashit( MY_PLUGIN_PATH ) . 'vendor/your_package/library_repo/LibraryFile.php';
In case you would put your library inside of your other folder such as includes
, you could change it to that path.
The method load_latest_path
will be used to load the latest registered library. This should be used after all plugins have been loaded so you’re sure that all the libraries have been loaded.
The last thing in our integration class, we will need to register the version on instantiating the class.
<?php
class Library_Integration {
// previous code here ...
/**
* Adding some support for Condition Engine such as filtering hooks for specific Conditions.
*/
public function __construct() {
$this->register_version();
}
}
Loading your Library in a Plugin
Let’s now see how we can load our library. This is just a simple example and you can change when and where to load it depending on your library and plugin.
<?php
// Somewhere in your plugin
include_once 'class-library-integration.php'
// We registered it.
$library_integration = new Library_Integration();
add_action( 'plugins_loaded', array( $this, 'load_library' );
function load_library() {
if ( ! class_exists('Library') ) {
Library_Integration::load_latest_path();
}
// this is your framework. Make sure you will call this after you loaded the latest path.
new Library();
}
Conclusion
By defining how your libraries will load you can make sure that all your plugins, that depend on it, will load the latest library.
Become a Sponsor
I see what you are trying to do, but I don’t see where the path is loaded into $GLOBALS as an array with the version number as the index.
Hi Manny, sorry about that. Seems that WP editor parsed that as a shortcode. I’ve changed the code using github gists so the correct code is displayed.
Hi Igor! Thanks for sharing this method of loading a library. How would you handle this if the library had multiple files?
For example, one could use something like this and then only have the other files in the library loaded if it is determined to be the latest library but then the autoloader doesn’t know about these files and one can get various errors.
On the other hand if one loads all the files using the autoloader one has bypassed the checks.
I suppose one option would be to add this sort of conditional loading logic to every file in the library, but that seems less than optimal!
Hi Dave, correct. I’ve built this for a library that loads all the files. But in case there only need to be loaded a few files, you can additional registrations where you register for example a “supports” array with file slugs (or packages). Then when loading, you can load all the packages that need to be supported.
Have you had experience using this technique along with including libraries using Composer?
I have written a tutorial on using libraries with composer: https://www.ibenic.com/using-composer-for-libraries-in-wordpress-plugins-or-themes/
This can be combined with that tutorial for a complete solution.